<?php
	global $AppUI;
	$project_id = dPgetParam($_GET, 'project_id');

	$perms = & $AppUI->acl();
	$canCreateLogs = $perms->checkModuleItem('task_log', 'add');
	require_once($AppUI->getModuleClass('tasks'));
	$expenseList = dPgetSysVal('TaskLogExpense');
	// Lets check which cost codes have been used before
//	$q  = new DBQuery;
//	$q->addQuery('project_company');
//	$q->addTable('projects');
//	$q->addWhere('project_id = ' . $project_id);
//	$company_id = $q->loadResult();
//
//	$q->addTable('billingcode');
//	$q->addQuery('billingcode_id, billingcode_name');
//	$q->addOrder('billingcode_name');
//	$q->addWhere('billingcode_status = 0');
//	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
//	$task_log_costcodes = $q->loadHashList();
//
// 	// Show deleted codes separately (at the end)
//	$q->addTable('billingcode');
//	$q->addQuery('billingcode_id, billingcode_name');
//	$q->addOrder('billingcode_name');
//	$q->addWhere('billingcode_status = 1');
//	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
//	$task_log_costcodes = arrayMerge(array('' => 'None'), $task_log_costcodes, $q->loadHashList());

//	$q  = new DBQuery;
//	$q->addTable('users', 'u');
//	$q->addQuery('user_id, concat(contact_first_name," ",contact_last_name)');
//	$q->addJoin('contacts', 'c', 'u.user_contact = c.contact_id');
//	$q->addJoin('project_contacts', 'p', 'p.contact_id = u.user_id');
//	$q->addOrder('contact_first_name, contact_last_name');
//	$q->addWhere('p.project_id = '.$project_id);


	$expense = dPgetParam( $_GET, 'expense');

// Winnow out the tasks we are not allowed to view.
$perms =& $AppUI->acl();
$project =& new CProject;

// Pull the task comments
$q  = new DBQuery;
$q->addTable('task_log');
$q->addQuery("task_log.*, DATE_FORMAT(task_log_date, '%Y-%m-%e') as task_log_date");
//$q->addQuery("task_log.*, DATE_FORMAT(task_log_date, '%Y-%m-%e') as task_log_date, user_username, t.task_id, t.task_name, t.task_is_complete, ite.item_name, divisions.div_name, cc.cc_name, t.task_percent_complete");
//$q->addQuery('billingcode_name as task_log_costcode');
//$q->addJoin('items', 'ite', 'ite.item_id = task_log_item');
//$q->addJoin('divisions', 'divisions', 'divisions.div_id = task_log_division');
//$q->addJoin('users', 'u', 'user_id = task_log_creator');
//$q->addJoin('costcenters', 'cc', 'cc.cc_id = task_log_costcenter');
//$q->addJoin('billingcode', 'b', 'task_log.task_log_costcode = billingcode_id');
$q->addWhere('task_log_project = '.$project_id);
if (is_array($expense) && count($expense) > 0) {
	$q->addWhere('task_log_expense in (' . implode(',', $_POST['expense']) . ')');
}
$needCheckCostCenter = $perms->userHasRole($AppUI->user_id, "FFIA_Staff") || $perms->userHasRole($AppUI->user_id, "Operation");
if ($needCheckCostCenter) {
	$q->addWhere("task_log_costcenter = {$AppUI->user_costcenter}");
}
$q->addOrder('task_log_id');

//$project->setAllowedSQL($AppUI->user_id, $q, 'task_project');

//echo $q->prepare();
$logs = $q->loadList();

header('Content-Type: text/xml');

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');

$xml->startElement('root');

obj2xml($xml, 'task_logs', 'log', $logs);

$xml->endElement();

echo $xml->outputMemory(true);
?>